package com.pengtu.web.app;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.WorkLog;
import com.pengtu.service.app.SystemManager;
import com.pengtu.service.app.WorkManager;
import com.pengtu.utils.DateUtils;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 工作日志
 * 
 * @author Administrator
 * 
 */
@Result(name=BaseActionSupport.RELOAD,location="worklog!list.action",type="redirect")
public class WorklogAction extends BaseActionSupport<WorkLog> {

	private static final long serialVersionUID = 1L;

	private WorkManager workManager;
	private WorkLog workLog;
	private String id;
	private Attachment attachmention;
	private SystemManager systemManager;
	private Page<WorkLog> page = new Page<WorkLog>(10);

	@Override
	public WorkLog getModel() {
		// TODO Auto-generated method stub
		return workLog;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		if (page.isOrderBySetted()) {
			page.order(Page.ASC);
			page.orderBy("createDate");
		}
		//queryfilter.addFilter("owner", SpringSecurityUtils.getCurrentUserName());
		page = workManager.findPageWorkLog(page, queryfilter);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		workLog.setCreateDate(DateUtils.getCurrentDate());
		//workLog.setOwner(SpringSecurityUtils.getCurrentUserName());
		workManager.saveWorkLog(workLog);
		if(null!=attachmention){
			attachmention.setTableId(workLog.getId());
			attachmention.setTableField("WorkLog_attachment");
			attachmention.setTableName("WorkLog");
			systemManager.uploadFile(Struts2Utils.getAppRootPath(), attachmention);
			workManager.saveWorkLog(workLog);
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		workManager.deleteWorkLog(id);
		return RELOAD;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			workLog = workManager.findAppWorkLog("id", id);
		} else {
			workLog = new WorkLog();
		}
	}

	@Autowired
	public void setWorkManager(WorkManager workManager) {
		this.workManager = workManager;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setWorkLog(WorkLog workLog) {
		this.workLog = workLog;
	}

	public Attachment getAttachmention() {
		return attachmention;
	}

	public void setAttachmention(Attachment attachmention) {
		this.attachmention = attachmention;
	}

	public Page<WorkLog> getPage() {
		return page;
	}

	public void setPage(Page<WorkLog> page) {
		this.page = page;
	}

	public String getId() {
		return id;
	}
	
	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}

}
